package su.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.user.dao.IUserGhsDAO;
import su.user.entity.UserGhs;


@Repository
public class UserGhsDAO extends SuBaseDAO implements IUserGhsDAO {
	
	
	public List<UserGhs> queryByPage(PagerDTO s) throws SQLException {
		String sql = "select * from su_user_ghs where status=1";
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql += " and id in ("+s.getS1()+")";
			}else{
				sql += " and id="+s.getS1();
			}
		}
		if(StringUtils.isNotBlank(s.getS2())){//
			sql += " and jianpin like '%"+s.getS2()+"%'";
		}
		sql += " ORDER BY id desc";
		
		return super.queryByPage(sql, new RowMapper<UserGhs>() {

			public UserGhs mapRow(ResultSet rs, int arg1) throws SQLException {
				UserGhs entity = new UserGhs();
				entity.setId(rs.getInt("id"));
				entity.setGhsname(rs.getString("ghsname"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setGhsdesc(rs.getString("ghsdesc"));
				return entity;
			}
			
		}, s);
	}
	
	
	public List<UserGhs> getAll() throws SQLException {
		String sql = "select * from su_user_ghs where status=1 order by id";
		
		return super.query(sql, new RowMapper<UserGhs>() {

			public UserGhs mapRow(ResultSet rs, int arg1) throws SQLException {
				UserGhs entity = new UserGhs();
				entity.setId(rs.getInt("id"));
				entity.setGhsname(rs.getString("ghsname"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setGhsdesc(rs.getString("ghsdesc"));
				return entity;
			}
			
		});
	}

	
	
	public UserGhs getById(int id) throws SQLException {
		String sql = "select * from su_user_ghs where status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<UserGhs>() {
			public UserGhs mapRow(ResultSet rs, int value) throws SQLException {
				UserGhs entity = new UserGhs();
				entity.setId(rs.getInt("id"));
				entity.setGhsname(rs.getString("ghsname"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setGhsdesc(rs.getString("ghsdesc"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_user_ghs set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public void insert(UserGhs o) throws SQLException {
		String sql = "insert into su_user_ghs(ghsname ,jianpin ,status ,ghsdesc ) values(? ,? ,? ,? )";
		super.update(sql, new Object[]{o.getGhsname() ,o.getJianpin() ,o.getStatus() ,o.getGhsdesc() });
	}
	
	
	
	public void update(UserGhs o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_user_ghs set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getGhsname());
		updateSql.append("ghsname=?").append(",");
		args.add(o.getJianpin());
		updateSql.append("jianpin=?").append(",");
		args.add(o.getStatus());
		updateSql.append("status=?").append(",");
		args.add(o.getGhsdesc());
		updateSql.append("ghsdesc=?");
			
		
		args.add(o.getId());
			
		
		String sql = updateSql + " where id = ? "; 
		super.update(sql, args.toArray());
	}

}

